package com.bidevalution.controller.api;

import com.bidevalution.business.context.EnterUserRoleType;
import com.bidevalution.business.context.UserRoleType;
import com.qtp.auth.api.UserService;
import com.qtp.common.constants.SystemConstants;
import com.qtp.common.core.ContextHolderUtils;
import com.qtp.common.core.TSUser;
import com.qtp.core.model.Response;
import lombok.extern.log4j.Log4j2;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Controller 基础类
 * @author shenh@glodon.com
 * @date 2018-2-28 16:30
 */
public abstract class BaseController {
    @Autowired(required = false)
    private UserService userService;
    Logger log = Logger.getLogger(getClass());
    public <T> T copyBean(T destObj,Object oriObject) {
    	try {
			BeanUtils.copyProperties(destObj, oriObject);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage(),new org.apache.cxf.common.i18n.Exception(e));
		} catch (InvocationTargetException e) {
			log.error(e.getMessage(),new org.apache.cxf.common.i18n.Exception(e));
		}
    	return destObj;
    }
    /**
     * 设置响应的内容类型以及编码方式
     * @param response 上下文响应对象
     */
    protected void setHttpServletResponse(HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-store");
    }

    /**
     * 获取登录用户信息
     * @return TSUser
     */
    protected TSUser getTSUser(){
        HttpSession session = ContextHolderUtils.getSession();
        return (TSUser)session.getAttribute(SystemConstants.USER_SESSION);
    }

    /**
     * 获得当前账号角色信息
     * @param loginUser
     * @param flag true ：返回所有的角色信息，faslse :返回其中一个
     * @return
     */
    protected List<String> getUserRole(TSUser loginUser) {

        String userGuid = loginUser.getUserGuid();
        List<String> roleList = new ArrayList<String>();
        Response<List<Map<String, Object>>> response1 = userService.getCurrentRole(userGuid);

        //未获得dubbo接口的信息，返回loginUser里的身份类型
//        if(response1 == null || response1.getResult() == null || response1.getResult().isEmpty()){
//            roleList.add(loginUser.getUserIdtype());
//            return roleList;
//        }

        List<Map<String, Object>> roles = response1.getResult();
        //从枚举类中获取身份类型
        for(UserRoleType type : UserRoleType.values()){
            for(Map<String,Object> map : roles) {
                if (type.getDesc().equals(map.get("role_name"))) {
                    roleList.add(type.getValue());
                }
            }
        }
        return roleList;
    }

    /**
     * 判断当前是否是企业用户
     * @param loginUser
     * @return
     */
    protected Boolean isEnterUserType(TSUser loginUser) {

        String userGuid = loginUser.getUserGuid();
        Response<List<Map<String, Object>>> response1 = userService.getCurrentRole(userGuid);

        //未获得dubbo接口的信息，返回loginUser里的身份类型
        if(response1 == null || response1.getResult() == null || response1.getResult().isEmpty()){
            return false;
        }

        List<Map<String, Object>> roles = response1.getResult();
        //从枚举类中获取身份类型
        for(EnterUserRoleType type : EnterUserRoleType.values()){
            for(Map<String,Object> map : roles) {
                if (type.getDesc().equals(map.get("role_name"))) {
                    return true;
                }
            }
        }
        return false;
    }
}
